import React from 'react';
import {injectIntl, language} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Table, Card, Divider, Spin} from 'antd';
import TextInput from 'Shared/components/TextInput';
import styles from './style.css';
import {PAGE, TABLE, PAGINATION_OPTIONS, FORM_ROW_OPTIONS, FORM_OPTIONS} from '../constants';
import WrappedTag from '../common/WrappedTag';
import difference from 'lodash/difference';
import {getHistroies, getPackDetails} from './api';
import {packingListType, partDeliveryHisType} from '../Enum';
import {conventEnumValueToString, uuid} from '../utils';
import routes from './routes';
import Return from '../common/Return';

import TagSelect from 'Shared/components/TagSelect';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';
const FormItem = Form.Item;
class History extends React.PureComponent {
    state = {
        condition: {
            code: '',
            partSaleOrderHisCode: '',
            partCode: '',
            preparationCode: '',
            partStatus: [],
        },
        conditionOfPage: {
            pageIndex: PAGE.index,
            pageSize: PAGE.smallSize,
        },
        //点击查询后的条件,用于分页查询
        list: {
            isFetching: false,
            total: 0,
            data: []
        },
    };
    handleFilterChange = (value, id) => {
        const newCondition = Object.assign({}, this.state.condition, {
            [id]: value
        });
        this.setState({
            condition: newCondition
        });
    };

    handleFilterPressEnter = (value, id) => {
        this.setState({
            condition: {
                ...this.state.condition,
                [id]: value
            }
        }, this.clickQueryBtn);
    };
    clickQueryBtn = () => {
        this.setState({
            conditionOfPage: Object.assign(
                {},
                this.state.conditionOfPage,
                {
                    pageIndex: PAGE.index
                },
                this.state.condition
            )
        });
        this.asyncQuery(
            Object.assign({}, this.state.condition, {
                pageSize: this.state.conditionOfPage.pageSize,
                pageIndex: PAGE.index,
                sortBy: this.state.conditionOfPage.sortBy,
                sortOrder: this.state.conditionOfPage.sortOrder
            })
        );
    };
    asyncQuery = condition => {
        this.setState({
            list: {
                ...this.state.list,
                isFetching: true
            }
        });
        const {sortBy, sortOrder, ...query} = condition;
        if(sortBy && sortOrder) {
            query.sortField = sortBy;
            query.isDesc = sortOrder === 'descend';
        }
        getHistroies(query).then(res => {
            if(res.ok)
                this.setState({
                    list: {
                        isFetching: false,
                        total: res.data.totalElements,
                        data: res.data.content
                    }
                });
            else
                this.setState({
                    list: {
                        isFetching: false,
                        total: 0,
                        data: []
                    }
                });
        });
    };
    onChangeSort = (pagination, filters, sorter) => {
        const {pageIndex, pageSize} = this.state.conditionOfPage;
        if(pagination.current - 1 === pageIndex && pagination.pageSize === pageSize) {
            const {field: sortBy, order: sortOrder} = sorter;
            this.setState({
                conditionOfPage: {
                    ...this.state.conditionOfPage,
                    sortBy,
                    sortOrder
                }
            }, this.clickQueryBtn);
        }
    };

    onPageSizeChange = (pageIndex, pageSize) => {
        const newCondition = Object.assign({}, this.state.conditionOfPage, {
            pageSize,
            pageIndex: PAGE.index
        });
        this.setState({
            conditionOfPage: newCondition
        });
        this.asyncQuery(newCondition);
    };
    onPageIndexChange = pageIndex => {
        const newCondition = Object.assign({}, this.state.conditionOfPage, {
            pageIndex: pageIndex - 1
        });
        this.setState({
            conditionOfPage: newCondition
        });
        this.asyncQuery(newCondition);
    };
    onClickReset = () => {
        this.setState({
            condition: {
                code: '',
                partSaleOrderHisCode: '',
                partCode: '',
                preparationCode: '',
                partStatus: [],
            }
        });
    }

    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.code',
                    defaultMessage: '调拨编号'
                }),
                sorter: true,
                dataIndex: 'code',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.partSaleOrderHisCode',
                    defaultMessage: '订单编号'
                }),
                sorter: true,
                dataIndex: 'partSaleOrderHisCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.partCode',
                    defaultMessage: '备件编号'
                }),
                sorter: true,
                dataIndex: 'partCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.partName',
                    defaultMessage: '备件名称'
                }),
                dataIndex: 'partName',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.partEngName',
                    defaultMessage: '备件英文名'
                }),
                dataIndex: 'partEngName',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.allocationQuantity',
                    defaultMessage: '调拨数量'
                }),
                dataIndex: 'allocationQuantity',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.receiptQuantity',
                    defaultMessage: '收货数量'
                }),
                dataIndex: 'receiptQuantity',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.preparationCode',
                    defaultMessage: '备料单号'
                }),
                dataIndex: 'preparationCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.partStatus',
                    defaultMessage: '调拨状态'
                }),
                sorter: true,
                dataIndex: 'partStatus',
                render: text => conventEnumValueToString(partDeliveryHisType, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.rreplacedPartCode',
                    defaultMessage: '被替换件'
                }),
                dataIndex: 'rreplacedPartCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'history.column.remark',
                    defaultMessage: '备注'
                }),
                dataIndex: 'remark',
                render: text => text && text.length > 50 ? <Ellipsis length={50} tooltip>
                    {text}
                </Ellipsis> : text
            },
        ];

        const pagination = {
            total: this.state.list.total,
            pageSize: this.state.conditionOfPage.pageSize,
            current: this.state.conditionOfPage.pageIndex + 1,
            onShowSizeChange: this.onPageSizeChange,
            onChange: this.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        return (
            <div>
                <Card>
                    <Form className="form-standard">
                        <Row>

                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'history.code',
                                        defaultMessage: '调拨编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput id="code" value={this.state.condition.code}
                                        onPressEnter={this.handleFilterPressEnter} onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'history.partSaleOrderHisCode',
                                        defaultMessage: '订单编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput id="partSaleOrderHisCode" value={this.state.condition.partSaleOrderHisCode}
                                        onPressEnter={this.handleFilterPressEnter} onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'history.partCode',
                                        defaultMessage: '备件编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput id="partCode" value={this.state.condition.partCode}
                                        onPressEnter={this.handleFilterPressEnter} onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'history.preparationCode',
                                        defaultMessage: '备料单号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput id="preparationCode" value={this.state.condition.preparationCode}
                                        onPressEnter={this.handleFilterPressEnter} onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.partStatus',
                                        defaultMessage: '状态'
                                    })}
                                    {...FORM_ROW_OPTIONS.item}>
                                    <TagSelect
                                        name="partStatus"
                                        value={this.state.condition.partStatus}
                                        options={partDeliveryHisType.toList(language)}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row className="operation-buttons">
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.clickQueryBtn} loading={this.state.list.isFetching}>
                                    {this.props.intl.formatMessage({
                                        id: 'history.btn.query',
                                        defaultMessage: '查询'
                                    })}
                                </Button>
                                <Button key="clear" onClick={this.onClickReset}>
                                    {this.props.intl.formatMessage({
                                        id: 'history.btn.reset',
                                        defaultMessage: '重置'
                                    })}
                                </Button>
                            </Col>
                        </Row>
                    </Form>
                </Card>
                <Card>
                    <Table
                        className="white-space-nowrap"
                        columns={columns}
                        dataSource={this.state.list.data}
                        rowKey="id"
                        pagination={pagination}
                        onChange={this.onChangeSort}
                        loading={this.state.list.isFetching}
                        {...TABLE} />
                </Card>
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.list.isFetching}
                        showWarning={false}
                        onConfirm={this.onCancel} />
                </div>
            </div>
        );
    }
}
History.propTypes = {
    history: PropTypes.object,
    intl: PropTypes.object,
};


export default injectIntl(History);


